const resumeModel = require('@/models/resume.model')
const JsonResult = require('@/utils/httpResponse.unit')
const { exportWebPageToPDF } = require('@/utils/exportFile')

class ResumeController {
  async createResume (req, response) {
    try {
      const { content, themeColor, themeStyleName } = req.body
      try {
        JSON.parse(content)
      } catch (error) {
        return JsonResult.fail({ req, response, error, message: 'content 数据格式错误' })
      }
      const userId = req.sessionuser.id
      const exists = await resumeModel.findOne({ userId })
      if (exists) {
        return JsonResult.fail({ req, response, message: '已存在一份简历，不允许重复创建' })
      }
      await resumeModel.create({
        userId, content, themeColor, themeStyleName
      })
      JsonResult.success({
        req,
        response,
        message: '新建简历成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '新建简历失败' })
    }
  }

  async updateResume (req, response) {
    const { content, themeColor, themeStyleName } = req.body
    try {
      try {
        JSON.parse(content)
      } catch (error) {
        return JsonResult.fail({ req, response, error, message: 'content 数据格式错误' })
      }
      const userId = req.sessionuser.id
      const exists = await resumeModel.findOne({ userId })
      if (!exists) {
        return JsonResult.fail({ req, response, message: '你还没有创建简历，不能更新' })
      }
      await resumeModel.update(userId, {
        content, themeColor, themeStyleName
      })
      JsonResult.success({
        req,
        response,
        message: '更新简历成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '更新简历失败' })
    }
  }

  async exportResume (req, response) {
    const userId = req.sessionuser.id
    try {
      const exists = await resumeModel.findOne({ userId })
      if (!exists) {
        return JsonResult.fail({ req, response, message: '你还没有创建简历，不能进行导出' })
      }
      const abuffer = await exportWebPageToPDF({
        url: `http://localhost:8088/preview-resume/${exists.id}`
      })
      response.set({
        'Content-Type': 'application/octec-stream',
        'Content-Disposition': 'attachment;'
      })
      response.write(abuffer)
      response.end()
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '导出简历失败' })
    }
  }

  async findResumeByUserId (req, response) {
    const userId = req.sessionuser.id
    try {
      const data = await resumeModel.findOneByUserId(userId)
      JsonResult.success({
        req,
        response,
        data,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询个人简历失败' })
    }
  }

  async findResumeById (req, response) {
    const id = req.query.id
    try {
      const data = await resumeModel.findOneById(id)
      JsonResult.success({
        req,
        response,
        data,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询简历失败' })
    }
  }
}

module.exports = new ResumeController()
